package com.gm.commonlib.util.numberUtils;

import android.text.TextUtils;


import com.gm.commonlib.util.stringUtils.StringUtils;

import java.math.BigDecimal;
import java.text.DecimalFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class NumberUtils {


    /**
     * 定义要显示的数字的格式
     */
    public final static String STYLE_1 = "0.0";
    /**
     * 在格式后添加诸如单位等字符
     */
    public final static String STYLE_2 = "00000.00 kg";
    /**
     * 模式中的"#"表示如果该位存在字符，则显示字符，如果不存在，则不显示。
     */
    public final static String STYLE_3 = "##0.00";
    /**
     * 模式中的"-"表示输出为负数，要放在最前面
     */
    public final static String STYLE_4 = "-000.000";
    /**
     * 模式中的","在数字中添加逗号，方便读数字
     */
    public final static String STYLE_5 = "-0,000.0#";
    /**
     * 模式中的"E"表示输出为指数，"E"之前的字符串是底数的格式， "E"之后的是字符串是指数的格式
     */
    public final static String STYLE_6 = "0.00E000";
    /**
     * 模式中的"%"表示乘以100并显示为百分数
     */
    public final static String STYLE_7 = "0.00%";
    /**
     * 模式中的"\u2030"表示乘以1000并显示为千分数;
     */
    public final static String STYLE_8 = "0.00\u2030";
    public final static String STYLE_9 = "##0.000";
    static final DecimalFormat decimalFormat = new DecimalFormat();
    private static final String[] CHINESE_NUMBER = {
            "零", "一", "二", "三", "四", "五",
            "六", "七", "八", "九",
    };
    private static final String[] CHINESE_NUMBER_UNIT = {
            "十", "百", "千", "万", "亿"
    };

    public static byte[] longToByte(long b) {
        long temp = b;
        byte[] var4 = new byte[8];

        for (int i = 0; i < 8; ++i) {
            var4[i] = (new Long(temp & 255L)).byteValue();
            temp >>= 8;
        }

        return var4;
    }

    public static long byteToLong(byte[] b) {
        long s0 = (long) (b[0] & 255);
        long s1 = (long) (b[1] & 255);
        long s2 = (long) (b[2] & 255);
        long s3 = (long) (b[3] & 255);
        long s4 = (long) (b[4] & 255);
        long s5 = (long) (b[5] & 255);
        long s6 = (long) (b[6] & 255);
        long s7 = (long) (b[7] & 255);
        s1 <<= 8;
        s2 <<= 16;
        s3 <<= 24;
        s4 <<= 32;
        s5 <<= 40;
        s6 <<= 48;
        s7 <<= 56;
        return s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;
    }

    public static byte[] intToByte(int temp) {
        temp = temp;
        byte[] b = new byte[4];

        for (int i = 0; i < 4; ++i) {
            b[i] = (new Integer(temp & 255)).byteValue();
            temp >>= 8;
        }

        return b;
    }

    public static int byteToInt(byte[] s3) {
        int s0 = s3[0] & 255;
        int s1 = s3[1] & 255;
        int s2 = s3[2] & 255;
        int s31 = (s3[3] & 255) << 24;
        s2 <<= 16;
        s1 <<= 8;
        return s0 | s1 | s2 | s31;
    }

    public static byte[] shortToByte(short temp) {
        int var3 = temp;
        byte[] b = new byte[2];

        for (int i = 0; i < 2; ++i) {
            b[i] = (new Integer(var3 & 255)).byteValue();
            var3 >>= 8;
        }

        return b;
    }

    public static short byteToShort(byte[] s1) {
        short s0 = (short) (s1[0] & 255);
        short s11 = (short) ((short) (s1[1] & 255) << 8);
        return (short) (s0 | s11);
    }

    public static int average(int x, int y) {
        return (x & y) + ((x ^ y) >> 1);
    }

    public static long getLongValue(Double d) {
        if (d == null) {
            return 0;
        }
        return Double.valueOf(d).longValue();
    }

    public static int getIntValue(Double d) {
        if (d == null) {
            return 0;
        }
        return Double.valueOf(d).intValue();
    }

    public static int getIntValue(String chars) {
        int r = 0;
        if (chars != null && chars.length() != 0) {
            StringBuffer bf = new StringBuffer();
            char[] var6 = chars.toCharArray();

            for (int i = 0; i < var6.length; ++i) {
                char c;
                if ((c = var6[i]) >= 48 && c <= 57) {
                    bf.append(c);
                } else if (c != 44 && bf.length() != 0) {
                    break;
                }
            }

            try {
                r = Integer.parseInt(bf.toString());
            } catch (Exception var5) {
                ;
            }
        }

        return r;
    }

    public static double getDoubleValue(String chars) {
        double d = 0.0D;
        if (chars != null && chars.length() != 0) {
            StringBuffer bf = new StringBuffer();
            char[] var7 = chars.toCharArray();

            for (int i = 0; i < var7.length; ++i) {
                char c;
                if ((c = var7[i]) >= 48 && c <= 57) {
                    bf.append(c);
                } else if (c == 46) {
                    if (bf.length() != 0) {
                        if (bf.indexOf(".") != -1) {
                            break;
                        }

                        bf.append(c);
                    }
                } else if (bf.length() != 0) {
                    break;
                }
            }

            try {
                d = Double.parseDouble(bf.toString());
            } catch (Exception var6) {
                ;
            }
        }

        return d;
    }

    public static String round(double val, int precision) {
        DecimalFormat df2;
        (df2 = new DecimalFormat()).setMaximumFractionDigits(precision);
        df2.setMinimumFractionDigits(precision);
        return df2.format(val);
    }

    public static Double roundDouble(double val, int precision) {
        Double ret = null;

        try {
            double factor = Math.pow(10.0D, (double) precision);
            ret = Double.valueOf(Math.floor(val * factor + 0.5D) / factor);
        } catch (Exception var6) {
            var6.printStackTrace();
        }

        return ret;
    }

    public static int parseDoubleToInt(double num) {
        return (new BigDecimal(num)).setScale(0, 4).intValue();
    }


    public static String formatFloatString(float str) {
        return formatFloatString(String.valueOf(str));
    }

    public static String formatFloatString(double str) {
        return formatFloatString(String.valueOf(str));
    }

    public static String formatFloatString(String str) {
        if (StringUtils.isEmpty(str)) {
            return "0";
        }
        BigDecimal decimal;
        try {
            decimal = new BigDecimal(str);
            decimal = decimal.setScale(2, BigDecimal.ROUND_HALF_UP);
        } catch (Exception e) {
            return str;
        }
        str = decimal.stripTrailingZeros().toPlainString();
        if (!str.contains(".")) {
            return str;
        }
        char[] chars = str.toCharArray();
        int i;
        for (i = str.length() - 1; i >= 0; i--) {
            if (chars[i] == '0') {

            } else {
                break;
            }
        }
        if (chars[i] != '.') {
            i++;
        }
        return str.substring(0, i);
    }

    public static void main(String[] args) {
        for (int i = 0; i < 10000; i++) {
            System.out.println(convertPositiveDigitToChineNum(i));
        }
    }

    public static String convertPositiveDigitToChineNum(int number) {
        if (number < 100) {
            return computePositiveDigitToChineNum(number, false);
        }
        return computePositiveDigitToChineNum(number, true);
    }

    private static String computePositiveDigitToChineNum(int number, boolean shouldAppendZero) {
        if (number < 0) {
            return "";
        }
        if (number < 10) {
            if (shouldAppendZero) {
                return CHINESE_NUMBER[0] + CHINESE_NUMBER[number];
            } else {
                return CHINESE_NUMBER[number];
            }
        }

        if (number < 100) {
            int remainNum = number % 10;
            if (remainNum == 0) {
                return CHINESE_NUMBER[number / 10] + CHINESE_NUMBER_UNIT[0];
            } else {
                return CHINESE_NUMBER[number / 10] + CHINESE_NUMBER_UNIT[0] + CHINESE_NUMBER[remainNum];
            }
        } else if (number < 1000) {
            int remainNum = number % 100;
            if (remainNum == 0) {
                return CHINESE_NUMBER[number / 100] + CHINESE_NUMBER_UNIT[1];
            } else {
                return CHINESE_NUMBER[number / 100] + CHINESE_NUMBER_UNIT[1]
                        + computePositiveDigitToChineNum(number % 100, true);
            }
        } else if (number < 10000) {
            int remainNum = number % 1000;
            if (remainNum == 0) {
                return CHINESE_NUMBER[number / 1000] + CHINESE_NUMBER_UNIT[2];
            } else {
                return CHINESE_NUMBER[number / 1000] + CHINESE_NUMBER_UNIT[2]
                        + computePositiveDigitToChineNum(number % 1000, true);
            }
        } else {
            return "";
        }
    }

    /**
     * 获取字符串中的数据
     *
     * @param data
     * @return
     */
    public static String getStringToNum(String data) {
        String regex = "[\u4e00-\u9fa5]";
        Pattern pat = Pattern.compile(regex);
        Matcher mat = pat.matcher(data);
        String repickStr = mat.replaceAll("").replaceAll("/", "");
        return repickStr;

    }

    public static String getChinese(String data) {
        if (TextUtils.isEmpty(data)) {
            return data;
        }
        String reg = "[^\u4e00-\u9fa5]";
        data = data.replaceAll(reg, " ");
        return data.trim();
    }

    public static boolean isCarNumber(String data) {
        Pattern pattern = Pattern.compile("^[\u4e00-\u9fa5]{1}[a-zA-Z]{1}[-]?[a-zA-Z0-9]{5}$");
        Matcher matcher = pattern.matcher(data);
        if (matcher.matches()) {
            return true;
        }
        return false;
    }

    public static boolean isZero(Integer value) {
        return value == null || value == 0;
    }

    public static boolean isZero(Long value) {
        return value == null || value == 0;
    }

    public static boolean isZero(Double value) {
        return value == null || value == 0 || value < 0.0001;
    }

    public static String subZeroAndDot(String s) {
        if (TextUtils.isEmpty(s)) {
            return "";
        }
        if (s.indexOf(".") > 0) {
            s = s.replaceAll("0+?$", "");//去掉多余的0
            s = s.replaceAll("[.]$", "");//如最后一位是.则去掉
        }
        return s;

    }

    /**
     * 数字每隔3位加逗号
     *
     * @param data
     * @return
     */
    public static String formatTosepara(double data) {
        data = DigitUtil.parseDouble(data, 2);
        DecimalFormat df = new DecimalFormat("#,##0.00");
        if (data == 0) {
            return "0";
        } else {
            return df.format(data);
        }
    }

    public static String formatTosepara3(double data) {
        data = DigitUtil.parseDouble(data, 2);
        DecimalFormat df = new DecimalFormat("0.00");
        if (data == 0) {
            return "0";
        } else {
            return NumberUtils.subZeroAndDot(df.format(data));
        }
    }

    //加千分号，默认保留4位小数
    public static String getMoneyStringWith4(double data) {
        data = DigitUtil.parseDouble(data, 4);
        DecimalFormat df = new DecimalFormat("#,##0.####");
        if (data == 0) {
            return "0";
        } else {
            return df.format(data);
        }
    }

    //加千分号，默认保留2位小数
    public static String getMoneyString(double data) {
        return formatTosepara3(data);
    }

    public static String getMoneyString(String data) {
        if (TextUtils.isEmpty(data)) return "";
        return formatTosepara3(Double.parseDouble(data));
    }

    // todo 转换成千分号的数据
    public static String getPermilNumber(double data, int bit) {
        data = DigitUtil.parseDouble(data, bit);
        DecimalFormat df = new DecimalFormat("#,##0.00");
        if (data == 0) {
            return "0.00";
        } else {
            return df.format(data);
        }
    }

    //加千分号，默认保留2位小数
    public static String getMoneyWithUnitStrig(double data) {
        return formatTosepara3(data) + " 元";
    }

    public static String getTowFloatData(double data) {
        data = DigitUtil.parseDouble(data, 2);
        DecimalFormat df = new DecimalFormat("0.00");
        if (data == 0) {
            return "0.00";
        } else {
            return df.format(data);
        }
    }



    //保留4位小数，不加千分号
    public static String getFourFloatData(double data) {
        data = DigitUtil.parseDouble(data, 4);
        DecimalFormat df = new DecimalFormat("0.0000");
        if (data == 0) {
            return "0.0000";
        } else {
            return df.format(data);
        }
    }

    public static String getFloatDataWithZero(double data, int bit) {
        data = DigitUtil.parseDouble(data, bit);
        String formt = "";
        if (bit == 1) {
            formt = "0.0";
        } else if (bit == 2) {
            formt = "0.00";
        } else if (bit == 3) {
            formt = "0.000";
        } else if (bit == 4) {
            formt = "0.0000";
        }
        DecimalFormat df = new DecimalFormat(formt);
        if (data == 0) {
            return formt;
        } else {
            return df.format(data);
        }
    }

    /**
     * 加法运算
     *
     * @param m1
     * @param m2
     * @return
     */
    public static double addDouble(double m1, double m2) {
        BigDecimal p1 = new BigDecimal(Double.toString(m1));
        BigDecimal p2 = new BigDecimal(Double.toString(m2));
        return p1.add(p2).doubleValue();
    }

    public static double addDouble(double... values) {
        BigDecimal tempResult = new BigDecimal(Double.toString(0));
        for (int i = 0; i < values.length; i++) {
            BigDecimal tempValue = new BigDecimal(Double.toString(values[i]));
            tempResult = tempResult.add(tempValue);
        }
        return tempResult.doubleValue();
    }

    /**
     * 减法运算
     *
     * @param m1
     * @param m2
     * @return
     */
    public static double subDouble(double m1, double m2) {
        BigDecimal p1 = new BigDecimal(Double.toString(m1));
        BigDecimal p2 = new BigDecimal(Double.toString(m2));
        return p1.subtract(p2).doubleValue();
    }

    /**
     * 除法运算
     *
     * @param m1
     * @param m2
     * @param scale
     * @return
     */
    public static double div(double m1, double m2, int scale) {
        if (scale < 0) {
            throw new IllegalArgumentException("Parameter error");
        }
        BigDecimal p1 = new BigDecimal(Double.toString(m1));
        BigDecimal p2 = new BigDecimal(Double.toString(m2));
        return p1.divide(p2, scale, BigDecimal.ROUND_HALF_UP).doubleValue();
    }

    //乘法
    public static double mul(double d1, double d2) {
        BigDecimal b1 = new BigDecimal(d1);
        BigDecimal b2 = new BigDecimal(d2);
        return b1.multiply(b2).doubleValue();
    }


    public static String getHindPhoneNumerString(String phone) {
        if (TextUtils.isEmpty(phone) || phone.length() < 11) {
            return phone;
        }
        return phone.replaceAll("(\\d{3})\\d{4}(\\d{4})", "$1****$2");
    }

    /**
     * 用户身份证号码的打码隐藏加星号加*
     * <p>18位和非18位身份证处理均可成功处理</p>
     * <p>参数异常直接返回null</p>
     *
     * @param idCardNum 身份证号码
     * @return 处理完成的身份证
     */
    public static String getHididCardNum(String idCardNum) {
        //身份证不能为空
        if (TextUtils.isEmpty(idCardNum)) {
            return idCardNum;
        }
        if (idCardNum.length() == 15) {
            return idCardNum.replaceAll("(\\d{4})\\d{7}(\\w{4})", "$1*****$2");
        } else if (idCardNum.length() == 18) {
            return idCardNum.replaceAll("(\\d{4})\\d{10}(\\w{4})", "$1*****$2");
        } else {
            return idCardNum;
        }

    }

    public static String getHindBlankNumberString(String blankCard, int startSize) {
        return blankCard;
//        if (TextUtils.isEmpty(blankCard)) {
//            return blankCard;
//        }
//        if (blankCard.length() < 8) {
//            return blankCard;
//        }
//        String startSizeString = "";
//        switch (startSize) {
//            case 1:
//                startSizeString = "*";
//                break;
//            case 2:
//                startSizeString = "**";
//                break;
//            case 3:
//                startSizeString = "***";
//                break;
//            case 4:
//                startSizeString = "****";
//                break;
//            default:
//                startSizeString = "****";
//                break;
//        }
//        return blankCard.substring(0, 4) + startSizeString + blankCard.substring(blankCard.length() - 4);
    }

    //去掉原来数据多余的0
    public static String getDataRemoveZero(double data, int bit) {
        data = DigitUtil.parseDouble(data, bit);
        if (data == 0) {
            return "0";
        } else {
            return subZeroAndDot(data + "");
        }
    }


    public static String getTwoBitString(double value) {
        decimalFormat.applyPattern(STYLE_3);
        return decimalFormat.format(value);
    }

    public static String getTwoBitString(float value) {
        decimalFormat.applyPattern(STYLE_3);
        return decimalFormat.format(value);
    }

    public static String getThreeBitString(double value) {
        decimalFormat.applyPattern(STYLE_9);
        return decimalFormat.format(value);
    }
}
